home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / ppl4c10.zip / WIN_IO.C < prev    next >
Text File  |  1995-02-11  |  5KB  |  246 lines

  1. /*
  2. **  -- win_io.c --
  3. **
  4. **  Implements non-overlapped screen I/O
  5. */
  6.  
  7. #define FALSE 0
  8. #define TRUE !FALSE
  9. #define BYTE unsigned char
  10.  
  11. #include <stdio.h>
  12. #include <dos.h>
  13. #include "dos_io.h"
  14. #include "win_io.h"
  15. #include "ascii.h"
  16.  
  17. #define NBR_WINS 3
  18.  
  19. #define CR 0x0d
  20. #define LF 0x0a
  21.  
  22. #define NORMAL  0x07
  23. #define INVERSE 0x70
  24.  
  25. static BYTE IsReady = FALSE;
  26. static BYTE Attribute[NBR_WINS];
  27. static BYTE Enabled[NBR_WINS];
  28. static BYTE CurRow[NBR_WINS];
  29. static BYTE CurCol[NBR_WINS];
  30. static BYTE TopRow[NBR_WINS];
  31. static BYTE BotRow[NBR_WINS];
  32. static BYTE LeftCol[NBR_WINS];
  33. static BYTE RghtCol[NBR_WINS];
  34.  
  35.  
  36. /*** PRIVATE ***/
  37.  
  38.  
  39. static void Initialize()
  40. {int i;
  41.  if(!IsReady)
  42.   {IsReady = TRUE;
  43.    for(i=0;i<NBR_WINS;i++) Enabled[i] = FALSE;
  44.   }
  45. }
  46.  
  47.  
  48. static int Check(int Win)
  49. {if(!IsReady) Initialize();
  50.  if((Win<0)||(Win>=NBR_WINS)) return WIN_RANGE;
  51.  if(Enabled[Win]) return WIN_AOK;
  52.  else return WIN_DISABLED;
  53. }
  54.  
  55.  
  56. static void NewRow(int Win)
  57. {if(++CurRow[Win]>BotRow[Win])
  58.    {Scroll(TopRow[Win],LeftCol[Win],BotRow[Win],RghtCol[Win],1,Attribute[Win]);
  59.     CurRow[Win] = BotRow[Win];
  60.    }
  61. }
  62.  
  63.  
  64. static void Advance(int Win)
  65. {if(++CurCol[Win]>RghtCol[Win])
  66.   {CurCol[Win] = LeftCol[Win];
  67.    NewRow(Win);
  68.   }
  69.  Position(CurRow[Win],CurCol[Win]);
  70. }
  71.  
  72.  
  73. static int WriteChar(int Win,char C)
  74. {
  75.  switch(C)
  76.    {case LF:
  77.       NewRow(Win);
  78.       /* fall thru */;
  79.     case CR:
  80.       CurCol[Win] = LeftCol[Win];
  81.       break;
  82.     case BS:
  83.       if(CurCol[Win] > LeftCol[Win])
  84.         {/* back up */
  85.          Position(CurRow[Win],--CurCol[Win]);
  86.          AttrWrite((BYTE)' ', (BYTE)Attribute[Win]);
  87.         }
  88.       break;
  89.     default:
  90.       if((C<' ')||(C>'~')) C = ' ';
  91.       AttrWrite((BYTE)C, (BYTE)Attribute[Win]);
  92.       Advance(Win);
  93.       break;
  94.    }
  95. return WIN_AOK;
  96. }
  97.  
  98.  
  99. /*** PUBLIC ***/
  100.  
  101.  
  102. /* set attribute to normal */
  103.  
  104. int WinNormal(int Win)
  105. {int rc;
  106.  if((rc=Check(Win))!=WIN_AOK) return rc;
  107.  Attribute[Win] = NORMAL;
  108.  return WIN_AOK;
  109. }
  110.  
  111.  
  112. /* set attribute to inverse */
  113.  
  114. int WinInverse(int Win)
  115. {int rc;
  116.  if((rc=Check(Win))!=WIN_AOK) return rc;
  117.  Attribute[Win] = INVERSE;
  118.  return WIN_AOK;
  119. }
  120.  
  121.  
  122. /* create window */
  123.  
  124.  
  125. int WinCreate(int Win,int r1,int c1,int r2,int c2)
  126. {if(!IsReady) Initialize();
  127.  if((Win<0)||(Win>=NBR_WINS)) return WIN_RANGE;
  128.  if(Enabled[Win]) return WIN_ENABLED;
  129.  Enabled[Win] = TRUE;
  130.  Attribute[Win] = NORMAL;
  131.  if(r1>r2) return FALSE;
  132.  if(r2>24) return FALSE;
  133.  if(c1>c2) return FALSE;
  134.  if(c2>79) return FALSE;
  135.  TopRow[Win] = r1;
  136.  BotRow[Win] = r2;
  137.  LeftCol[Win] = c1;
  138.  RghtCol[Win] = c2;
  139.  CurRow[Win] = r1;
  140.  CurCol[Win] = c1;
  141.  Position((BYTE)r1,(BYTE)c1);
  142.  return WIN_AOK;
  143. }
  144.  
  145. /* write char to window */
  146.  
  147. int WinPutChar(int Win,char C)
  148. {int rc;
  149.  if((rc=Check(Win))!=WIN_AOK) return rc;
  150.  Position(CurRow[Win],CurCol[Win]);
  151.  return WriteChar(Win,C);
  152. }
  153.  
  154. /* write string to window */
  155.  
  156. int WinPutString(int Win,char *String)
  157. {int rc;
  158.  if((rc=Check(Win))!=WIN_AOK) return rc;
  159.  Position(CurRow[Win],CurCol[Win]);
  160.  while(*String!='\0') WriteChar(Win,*String++);
  161.  return WIN_AOK;
  162. }
  163.  
  164. /* clear window */
  165.  
  166. int WinClear(int Win)
  167. {int rc;
  168.  if((rc=Check(Win))!=WIN_AOK) return rc;
  169.  Scroll(TopRow[Win],LeftCol[Win],BotRow[Win],RghtCol[Win],0,Attribute[Win]);
  170.  CurRow[Win] = TopRow[Win];
  171.  CurCol[Win] = LeftCol[Win];
  172.  return WIN_AOK;
  173. }
  174.  
  175. /* get string from window */
  176.  
  177. int WinGetString(int Win, char *String, int Length)
  178. {int  i, rc;
  179.  char c;
  180.  if((rc=Check(Win))!=WIN_AOK) return rc;
  181.  Position(CurRow[Win],CurCol[Win]);
  182.  /* input text from user */
  183.  i = 0;
  184.  while(1)
  185.      {c = ReadKbd();
  186.       switch(c)
  187.         {case LF:
  188.            /* ignore LFs */
  189.            break;
  190.          case CR:
  191.            /* done */
  192.            String[i] = '\0';
  193.            return WIN_AOK;
  194.          case ESC:
  195.          case CAN:
  196.            /* aborting */
  197.            *String = '\0';
  198.            return WIN_AOK;
  199.          case BS:
  200.            if(i>0)
  201.              {/* back up */
  202.               i--;
  203.               Position(CurRow[Win],--CurCol[Win]);
  204.               AttrWrite((BYTE)' ', (BYTE)Attribute[Win]);
  205.              }
  206.            break;
  207.          default:
  208.            /* save character & display on status line */
  209.            if((c>=' ')&&(c<='~'))
  210.               {String[i++] = c;
  211.                AttrWrite((BYTE)c, (BYTE)Attribute[Win]);
  212.                Advance(Win);
  213.                if(i==Length)
  214.                   {/* user string done */
  215.                    String[i] = '\0';
  216.                    return WIN_AOK;
  217.                   }
  218.               }
  219.            break;
  220.         } /* end case */
  221.      } /* end while */
  222. }
  223.  
  224. /* set cursor position in window */
  225.  
  226. int WinSetPos(int Win,int Row, int Col)
  227. {int rc;
  228.  if((rc=Check(Win))!=WIN_AOK) return rc;
  229.  if(TopRow[Win]  + Row > BotRow[Win])  return WIN_RANGE;
  230.  if(LeftCol[Win] + Col > RghtCol[Win]) return WIN_RANGE;
  231.  CurRow[Win] = TopRow[Win]+Row;
  232.  CurCol[Win] = LeftCol[Win]+Col;
  233.  Position(CurRow[Win],CurCol[Win]);
  234.  return WIN_AOK;
  235. }
  236.  
  237. /* get window cursor position */
  238.  
  239. int WinGetPos(int Win,int *RowP,int *ColP)
  240. {int rc;
  241.  if((rc=Check(Win))!=WIN_AOK) return rc;
  242.  Position(CurRow[Win],CurCol[Win]);
  243.  if(RowP) *RowP = CurRow[Win] - TopRow[Win];
  244.  if(ColP) *ColP = CurCol[Win] - LeftCol[Win];
  245.  return WIN_AOK;
  246. }